package com.fk.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fk.reggie.common.CustomException;
import com.fk.reggie.domain.Category;
import com.fk.reggie.domain.Dish;
import com.fk.reggie.domain.Setmeal;
import com.fk.reggie.mapper.DishMapper;
import com.fk.reggie.mapper.SetmealMapper;
import com.fk.reggie.service.CategoryService;
import com.fk.reggie.mapper.CategoryMapper;
import com.fk.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 凡凯
 * @description 针对表【category(菜品及套餐分类)】的数据库操作Service实现
 * @createDate 2023-03-15 21:53:40
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
        implements CategoryService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    public void deleteCategory(Long id) {
        //1.判断当前category有没有dish
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        Integer dishCount = dishMapper.selectCount(dishLambdaQueryWrapper);
        if (dishCount > 0) {
            throw new CustomException("该分类下有菜品，不能删除");
        }
        //2.判断当前category有没有Setmeal
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        Integer setmealCount = setmealMapper.selectCount(setmealLambdaQueryWrapper);
        if (setmealCount > 0) {
            throw new CustomException("该分类下有套餐，不能删除");
        }
        //删除分类
        categoryMapper.deleteById(id);
    }

}




